package com.lalosoft.neodatisDemo;

import java.io.File;

import org.neodatis.odb.ODB;
import org.neodatis.odb.ODBFactory;
import org.neodatis.odb.OID;
import org.neodatis.odb.Objects;
import org.neodatis.odb.core.query.IQuery;
import org.neodatis.odb.core.query.criteria.Where;
import org.neodatis.odb.impl.core.query.criteria.CriteriaQuery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.lalosoft.neodatisDemo.dao.City;
import com.lalosoft.neodatisDemo.dao.Country;
import com.lalosoft.neodatisDemo.dao.People;

public class DBNeoDatisDemoActivity extends Activity {
	
	private static String TAG = "PrincipalActivity";
    
	private TextView tv;
	String fileName;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.text_view);
        createFile();
        storeData(loadCountry());
        showFullData();
        try {
			clearDataBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    private void createFile(){
    	 // Ask Android where we can store our file
        File directory = getDir("data", Context.MODE_PRIVATE);
        fileName = directory.getAbsolutePath() + "/test-android.neodatis";
        Log.i(TAG, "File created: " + fileName);
    }
    
    private void storeData(Country aCountry){
    	ODB odb = null;
    	try{
    		// Open the database
			odb = ODBFactory.open(fileName);
			
			// Store the object
			OID oid = odb.store(aCountry);
			Log.i(TAG, "Data stored: " + oid.toString());
			
    	} finally {
			if (odb != null) {
				// Close the database
				odb.close();
			}
		}
    }
    
    private void showFullData(){
    	ODB odb = null;

		try {
			// Open the database
			odb = ODBFactory.open(fileName);
			Objects <Country> countries = odb.getObjects(Country.class);
			StringBuilder sb = new StringBuilder();
			sb.append(countries.getFirst().toString());
			
			// Query --> all people with 25 years
			IQuery query = new CriteriaQuery(People.class, Where.equal("age", "25"));
			Objects<People> peoples = odb.getObjects(query);
			sb.append("\n").append("People 25 year").append("\n");
			sb.append(peoples);
			
			// Sorting Query results
			query.orderByDesc("lastName");
			peoples = odb.getObjects(query);
			sb.append("\n").append("People 25 year - Sort by last name").append("\n");
			sb.append(peoples);
			
			tv.setText(sb.toString());
			Log.i(TAG, "Show data");
		} finally {
			if (odb != null) {
				// Close the database
				odb.close();
			}
		}
    }
    
    
    private Country loadCountry(){
    	Country country = new Country("Argentina");
    	
    	country.getCities().add(loadCity1());
    	country.getCities().add(loadCity2());

    	return country;
    }
    
    private void clearDataBase() throws Exception {
		Objects objects = null;

		// Open the database
		ODB odb = null;

		try {
			odb = ODBFactory.open(fileName);
			objects = odb.getObjects(City.class);
			while (objects.hasNext()) {
				odb.delete(objects.next());
			}
			Log.i(TAG, "Data City deleted!");
			objects = odb.getObjects(People.class);
			while (objects.hasNext()) {
				odb.delete(objects.next());
			}
			Log.i(TAG, "Data People deleted!");
			objects = odb.getObjects(Country.class);
			while (objects.hasNext()) {
				odb.delete(objects.next());
			}
			Log.i(TAG, "Data Country deleted!");
		} finally {
			if (odb != null) {
				// Close the database
				odb.close();
			}
		}
	}
    
    private City loadCity1(){
    	City city = new City("Buenos Aires");
    	
    	People p1 = new People("Juan", "Peres");
    	p1.setAge("40");
    	People p2 = new People("Pedro", "Ramirez");
    	p2.setAge("10");
    	People p3 = new People("Jose", "Sanchez");
    	p3.setAge("25");
    	People p4 = new People("Miguel", "Suarez");
    	p4.setAge("49");
    	People p5 = new People("David", "Fernandez");
    	p5.setAge("25");
    	
    	city.getPeoples().add(p1);
    	city.getPeoples().add(p2);
    	city.getPeoples().add(p3);
    	city.getPeoples().add(p4);
    	city.getPeoples().add(p5);
    	
    	return city;
    }
    
    private City loadCity2(){
    	City city = new City("Cordoba");
    	
    	People p1 = new People("Juan", "Peres");
    	p1.setAge("40");
    	People p2 = new People("Pedro", "Ramirez");
    	p2.setAge("10");
    	People p3 = new People("Jose", "Sanchez");
    	p3.setAge("25");
    	People p4 = new People("Miguel", "Suarez");
    	p4.setAge("49");
    	People p5 = new People("David", "Fernandez");
    	p5.setAge("21");
    	
    	city.getPeoples().add(p1);
    	city.getPeoples().add(p2);
    	city.getPeoples().add(p3);
    	city.getPeoples().add(p4);
    	city.getPeoples().add(p5);
    	
    	return city;
    }
    
}